cc.Class({
    extends: cc.Component,

    properties: {
        faceSpts: [cc.SpriteFrame]
    },

    onLoad() {
        this.face = this.node.find('motor_face', cc.Sprite);
        this.face.node.setScale(0);
        this.gas = this.node.find('TailGas', cc.Animation);
        this.dust = this.node.find('TailDust', cc.Animation);
        this.arma = this.getComponent(dragonBones.ArmatureDisplay);
        this.readyMove();
    },

    readyMove() {
        this.node.x = 0;
        this.node.stopAllActions();
        this.playState(GLOBAL.STATE_STILL);
    },

    startMove(data, timer) {
        this.moveData = data;
        this.moveStep = 0;
        timer ? this.adjustMove(timer) : this.onNextMove();
    },

    adjustMove(timer) {
        var addT = 0;
        var addS = 0;
        var data = this.moveData;
        var len = data.times.length;
        for (var i = 0; i < len; i++) {
            addT += data.times[i];
            addS += data.moves[i];
            this.moveStep = i;
            if (addT > timer) break;
        }
        if (this.moveStep < len - 1) {
            var dt = addT - timer;
            var ds = dt / data.times[this.moveStep] * data.moves[this.moveStep];
            this.node.x = (addS - ds) * GLOBAL.FACTOR_MOVE;
            this.playState(data.states[this.moveStep], dt, ds);
            this.moveStep++;
        } else {
            this.node.x = addS * GLOBAL.FACTOR_MOVE;
            this.playState(data.states[this.moveStep]);
            this.captureMove();
        }
    },

    captureMove() {
        this.gas.pause();
        this.dust.pause();
        this.arma.armature().animation.stop(this.arma.animationName);
        this.node.emit('MOVE_CAPTURE', this);
    },

    sprintMove() {
        this.gas.resume();
        this.dust.resume();
        this.arma.armature().animation.play();
        this.node.runAction(cc.moveBy(1.2 * GLOBAL.FACTOR_TIME, 1280 * 1.2, 0));
    },

    onNextMove() {
        var d = this.moveData;
        var s = this.moveStep;
        if (s < d.times.length) {
            this.playState(d.states[s], d.times[s], d.moves[s]);
            this.moveStep++;
        } else {
            this.captureMove();
            this.face.node.stopAllActions();
            this.face.node.setScale(0);
        }
    },

    onComplete() {
        this.arma.removeEventListener('complete', this.onComplete, this);
        this.playState(GLOBAL.STATE_MOVE);
    },

    playAnims(motor, gas, dust, once) {
        this.arma.playAnimation(motor, once ? 1 : -1);
        this.gas.play(gas);
        this.dust.play(dust);
        once && this.arma.addEventListener('complete', this.onComplete, this);
    },

    playState(state, duration, movement) {
        if (duration) {
            if(state != GLOBAL.STATE_FALL_HEAD) {
                this.node.runAction(cc.sequence(
                    cc.moveBy(duration * GLOBAL.FACTOR_TIME, movement * GLOBAL.FACTOR_MOVE, 0),
                    cc.callFunc(this.onNextMove, this)
                ));
            } else {
                this.node.runAction(cc.sequence(
                    cc.moveBy((duration - 0.6) * GLOBAL.FACTOR_TIME, movement * GLOBAL.FACTOR_MOVE, 0),
                    cc.delayTime(0.6),
                    cc.callFunc(this.onNextMove, this)
                ));
            }
        }
        switch (state) {
            case GLOBAL.STATE_STILL:
                this.playAnims('启动', 'weiqi_tiaoyue', 'yw_tiaoyue');
                break;
            case GLOBAL.STATE_MOVE:
            case GLOBAL.STATE_DECEL_MOVE:
                this.playAnims('正常前进', 'weiqi_tiaoyue', 'yw_zhengchang');
                this.playFace(3, 0);
                break;
            case GLOBAL.STATE_JUMP:
                this.playAnims('跳跃加速', 'weiqi_tiaoyue', 'yw_tiaoyue', true);
                this.playFace(2, 1);
                break;
            case GLOBAL.STATE_DECEL:
                this.playAnims('减速', 'weiqi_tiaoyue', 'yw_jiansu');
                this.playFace(0, 0);
                break;
            case GLOBAL.STATE_ACCEL:
                this.playAnims('加速后_前进', 'weiqi_jiasu', 'yw_jiasu');
                this.playFace(1, 0);
                break;
            case GLOBAL.STATE_SPRINT:
                this.playAnims('加速后_冲刺', 'weiqi_jiasu', 'yw_jiasu', true);
                this.playFace(2, 0);
                break;
            case GLOBAL.STATE_FALL_HEAD:
                this.playAnims('翻车_前翻', 'weiqi_tiaoyue', 'yw_tiaoyue', true);
                break;
            case GLOBAL.STATE_FALL_BACK:
                this.playAnims('翻车_后翻', 'weiqi_tiaoyue', 'yw_tiaoyue', true);
                break;
        }
    },

    playFace(spt, delay) {
        if(!this.face.spriteFrame && this.moveStep < this.moveData.times.length) {
            this.face.spriteFrame = this.faceSpts[spt];
            this.face.node.stopAllActions();
            this.face.node.runAction(cc.sequence(
                cc.delayTime(delay),
                cc.scaleTo(0.2, 1, 1).easing(cc.easeBackOut()),
                cc.delayTime(0.6),
                cc.scaleTo(0.2, 0, 0).easing(cc.easeBackIn()),
                cc.delayTime(2),
                cc.callFunc(() => this.node.emit('EMOJI_END'), this)
            ))
        }
    },

    playEmoji() {
        this.face.spriteFrame = null;
    }

});
